<?php

use Ramsey\Uuid\Uuid;
use App\Common\ResponseCode;
use Illuminate\Support\Facades\Log;

/*
 * 格式化参数，签名过程需要使用
 */
function formatParam($paraMap, $urlencode = false)
{
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v)
    {
        if ($urlencode)
        {
            $v = urlencode($v);
        }
        $buff .= $k . "=" . $v . "&";
    }
    $reqPar = '';
    if (strlen($buff) > 0)
    {
        $reqPar = substr($buff, 0, strlen($buff) - 1);
    }
    return $reqPar;
}


/**
 * 生成uuid
 * @return mixed
 */
function getUuid()
{
    $temp = Uuid::uuid1();
    return $temp->getHex();
}

/**
 * api响应
 * @param  string  $data
 * @param  int     $code
 * @param  string  $message
 * @return \Illuminate\Http\JsonResponse
 */
function apiResponse($data = [], $code = ResponseCode::SUCCESS, $message = '请求成功')
{
    if (!is_null($data) && empty($data))
    {
        $data = [];
    }
    if ($code != ResponseCode::SUCCESS && empty($data))
    {
        $data = null;
    }
    $response = array(
        'code' => $code,
        'msg' => $message,
        'data' => $data
    );
    return response()->json($response);
}

/**
 * 加密解密码
 * @param  mixed  $string
 * @param  mixed  $operation  D解密 E加密
 * @param  mixed  $key
 * @return mixed
 */
function anniEncrypt($string, $operation)
{
    $operation = trim(strtoupper(trim($operation)));
    $key = "anniekids jockerwang";//把密码加长防止逆向解密
    $key = md5($key);
    $key_length = strlen($key);
    $string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
    $string_length = strlen($string);
    $rndkey = $box = array();
    $result = '';
    for ($i = 0; $i <= 255; $i++)
    {
        $rndkey[$i] = ord($key[$i % $key_length]);
        $box[$i] = $i;
    }
    for ($j = $i = 0; $i < 256; $i++)
    {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for ($a = $j = $i = 0; $i < $string_length; $i++)
    {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if ($operation == 'D')
    {
        if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8))
        {
            return substr($result, 8);
        }
        else
        {
            return '';
        }
    }
    else
    {
        return str_replace('=', '', base64_encode($result));
    }
}


/**
 * 处理富文本地址域名
 * @param  string  $string
 * @param  string  $operation  D：去除oss地址   E：添加oss地址
 * @return mixed|string
 */
function ossUrlReplace($string = '', $operation = 'E')
{
    if (empty($string))
    {
        return '';
    }
    //判断环境
    $needle = '/resource/';
    if (strpos($string, '/testResource/'))
    {
        $needle = '/testResource/';
    }
    //处理数据
    if ($operation == 'D')
    {
        $string = str_replace(MPFILE_PATH . $needle, $needle, $string);
    }
    else
    {
        $string = str_replace($needle, MPFILE_PATH . $needle, $string);
    }
    return $string;
}

function getOutTradeNo($parentId, $deviceType, $out_prefix)
{
    $deviceTypeP = 0;
    switch ($deviceType)
    {
        case 'iOS':
            $deviceTypeP = 1;
            break;
        case 'android':
            $deviceTypeP = 2;
            break;
        case 'iOSHD':
            $deviceTypeP = 3;
            break;
        case 'androidHD':
            $deviceTypeP = 4;
            break;
        default:
            $deviceTypeP = 1;
    }
    //商户订单编号
    $orderSn = $out_prefix . date('Ymd') . $deviceTypeP . substr(microtime(), 2, 5) .
        sprintf('%02d', rand(0, 99)) . str_pad(substr($parentId, -4), 4, 0, STR_PAD_LEFT);
    return $orderSn;
}
